﻿/*****************************************************************************\
 *                            RESTRICTED RIGHTS LEGEND
 *
 *      Use, duplication, or disclosure by the Government is subject to
 *      restrictions as set forth in subparagraph (c) (1) (ii) of the Rights in
 *      Technical Data and Computer Software Clause at DFARS 252.227-7013.
 * 
 *      Copyright © 2012,2013, The Board of Trustees of the University of Illinois. All rights reserved.
 *
 *      Developed by: Erich Heine, David Rodgers, Denis Kholine, Derek Dagit, William H. Sanders, 
 *                    Klara Nahrstedt, Rakesh Bobba, Tim Yardley
 *
 *      Information Trust Institute
 * 
 *      University of Illinois
 *      
 *      The Board of Trustees of the University of Illinois
 *      109 Coble Hall
 *      801 South Wright Street
 *      Champaign, IL 61820-6242
 *
 *      http://www.iti.illinois.edu
 * 
 *      Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
 *      associated documentation files (the “Software”), to deal with the Software without restriction, 
 *      including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *      and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 *      subject to the following conditions:
 *
 *      Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *      following disclaimers.
 *      
 *      Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
 *      the following disclaimers in the documentation and/or other materials provided with the distribution.
 *
 *      Neither the names of Information Trust institute, University of Illinois, The Board of Trustees of the
 *      University of Illinois nor the names of its contributors may be used to endorse or promote products 
 *      derived from this Software without specific prior written permission.
 *      
 *      THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 *      LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN
 *      NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *      WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 *      SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 *      
 \*****************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using GSF;

namespace SIEGate.Core.Legacy
{
    public interface ILegacyMeasurement : IMeasurement
    {
        [Obsolete]
        Guid ID
        {
            get;
            set;
        }

        [Obsolete]
        MeasurementKey Key
        {
            get;
            set;
        }

        [Obsolete]
        string TagName
        {
            get;
            set;
        }

        [Obsolete]
        double AdjustedValue
        {
            get;
        }

        [Obsolete]
        double Adder
        {
            get;
            set;
        }

        [Obsolete]
        double Multiplier
        {
            get;
            set;
        }

        [Obsolete]
        Ticks ReceivedTimestamp
        {
            get;
            set;
        }

        [Obsolete]
        Ticks PublishedTimestamp
        {
            get;
            set;
        }

        [Obsolete]
        MeasurementValueFilterFunction MeasurementValueFilter
        {
            get;
            set;
        }
    }

    /// <summary>
    /// Defines static extension functions for <see cref="ILegacyMeasurement"/> implementations.
    /// </summary>
    /// <remarks>
    /// These helper functions map to the previously defined corresponding properties to help with the transition of <see cref="MeasurementStateFlags"/>.
    /// </remarks>
    public static class ILegacyMeasurementExtensions
    {
        /// <summary>
        /// Returns <c>true</c> if <see cref="MeasurementStateFlags.BadData"/> is not set.
        /// </summary>
        /// <param name="measurement"><see cref="ILegacyMeasurement"/> instance to test.</param>
        /// <returns><c>true</c> if <see cref="MeasurementStateFlags.BadData"/> is not set.</returns>
        public static bool ValueQualityIsGood(this ILegacyMeasurement measurement)
        {
            return (measurement.StateFlags & MeasurementStateFlags.BadData) == 0;
        }

        /// <summary>
        /// Returns <c>true</c> if <see cref="MeasurementStateFlags.BadTime"/> is not set.
        /// </summary>
        /// <param name="measurement"><see cref="ILegacyMeasurement"/> instance to test.</param>
        /// <returns><c>true</c> if <see cref="MeasurementStateFlags.BadTime"/> is not set.</returns>
        public static bool TimestampQualityIsGood(this ILegacyMeasurement measurement)
        {
            return (measurement.StateFlags & MeasurementStateFlags.BadTime) == 0;
        }

        /// <summary>
        /// Returns <c>true</c> if <see cref="MeasurementStateFlags.DiscardedValue"/> is set.
        /// </summary>
        /// <param name="measurement"><see cref="ILegacyMeasurement"/> instance to test.</param>
        /// <returns><c>true</c> if <see cref="MeasurementStateFlags.DiscardedValue"/> is not set.</returns>
        public static bool IsDiscarded(this ILegacyMeasurement measurement)
        {
            return (measurement.StateFlags & MeasurementStateFlags.DiscardedValue) > 0;
        }

        /// <summary>
        /// Returns <c>true</c> if <see cref="MeasurementStateFlags.CalcuatedValue"/> is set.
        /// </summary>
        /// <param name="measurement"><see cref="ILegacyMeasurement"/> instance to test.</param>
        /// <returns><c>true</c> if <see cref="MeasurementStateFlags.CalcuatedValue"/> is not set.</returns>
        public static bool IsCalculated(this ILegacyMeasurement measurement)
        {
            return (measurement.StateFlags & MeasurementStateFlags.CalcuatedValue) > 0;
        }

        /// <summary>
        /// Returns the measurement ID if defined, otherwise the run-time signal ID associated with the measurement key.
        /// </summary>
        /// <param name="measurement"><see cref="ILegacyMeasurement"/> instance to test.</param>
        /// <returns>Measurement ID if defined, otherwise the run-time signal ID associated with the measurement key.</returns>
        public static Guid RuntimeSignalID(this ILegacyMeasurement measurement)
        {
            return measurement.ID == Guid.Empty ? measurement.Key.SignalID : measurement.ID;
        }

        /// <summary>
        /// Returns the <see cref="MeasurementKey"/> values of a <see cref="ILegacyMeasurement"/> enumeration.
        /// </summary>
        /// <param name="measurements"><see cref="ILegacyMeasurement"/> enumeration to convert.</param>
        /// <returns><see cref="MeasurementKey"/> values of the <see cref="ILegacyMeasurement"/> enumeration.</returns>
        public static IEnumerable<MeasurementKey> MeasurementKeys(this IEnumerable<ILegacyMeasurement> measurements)
        {
            return measurements == null ? new MeasurementKey[0] : measurements.Select(m => m.Key);
        }
    }
}
